package com.sdwl.brcl.validation.validation;

import java.math.BigDecimal;

/**
 * @author xied
 * @date 2021年06月11日 14:11
 */
public class NumberValidator implements Validator {
    public static final int GT = 0;
    public static final int LT = 1;
    public static final int GT_EQ = 2;
    public static final int LT_EQ = 3;
    public static final int EQ = 4;
    private int type;

    public NumberValidator(int type) {
        this.type = type;
    }

    @Override
    public boolean validate(Object value) {
        Object[] values = (Object[]) ((Object[]) value);
        BigDecimal val = new BigDecimal(String.valueOf(values[0]));
        BigDecimal expectVal = new BigDecimal(String.valueOf(values[1]));
        int i = val.compareTo(expectVal);
        switch (this.type) {
            case 0:
                return i > 0;
            case 1:
                return i < 0;
            case 2:
                return i >= 0;
            case 3:
                return i <= 0;
            case 4:
                return i == 0;
            default:
                throw new UnsupportedOperationException("不支持的数字对比操作");
        }
    }

    @Override
    public String validateCode() {
        switch (this.type) {
            case 0:
                return "must.gt";
            case 1:
                return "must.lt";
            case 2:
                return "must.gt.eq";
            case 3:
                return "must.lt.eq";
            case 4:
                return "must.eq";
            default:
                return "";
        }
    }

    @Override
    public String validateMsg(Object value, String validateName) {
        Object[] values = (Object[]) ((Object[]) value);
        switch (this.type) {
            case 0:
                return validateName + "必须大于" + values[1];
            case 1:
                return validateName + "必须小于" + values[1];
            case 2:
                return validateName + "必须大于等于" + values[1];
            case 3:
                return validateName + "必须小于等于" + values[1];
            case 4:
                return validateName + "必须等于" + values[1];
            default:
                return "";
        }
    }
}
